Adding Multiple Slider Controls to Your Windows

by Mike Bradbury

mike@karemi.fsnet.co.uk


Home

Text To Speech - Pendl

Novice Puzzles - Terra

Slider Controls - Bradbury

Symmetrical Paint - Nally

Programming Style - Brossman

Progress Simulator - Rahman

CD Menus - Sweet

Media File Search - Sweet

Why Liberty BASIC? - Sweet

Submission Guildlines

Newsletter Help

Index

Code for Slider Controls Made Easy With LB4

The code provided below, is valid only for Liberty BASIC Version 4. This is an example of how much, code can be abbreviated by switching to LB4, well worth doing even if you only write code for your own use.

The main 'assistor' for simplifying the slider code are handle variables, which enable a single function to redraw any number of sliders you may wish to use. When the mouse pointer is inside a slider and the left button is held down, the chosen slider is identified by a GetFocus call to user32.dll to retrieve the slider graphicbox handle.

The handle is then compared with the handles obtained after the window was opened in a for/next loop. The value of the counter i when a match is found, gives the selected slider number, the value of which is passed to the function

so it knows which slider to redraw when the mouse is moved. By converting the slider number into a string, many later conversions are avoided, where the slider number is to be used in a handle variable.

It is a simple matter to increase the number of sliders by increasing the value of numSliders and adding the definitions for the additional controls. I have made it even easier for you by including rem'd lines in the appropriate places.

Program Listing

Code follows: copy/paste into LB4 editor and run


'Slider controls for LB4
'LB newsletter submission by Mike Bradbury, March 2004 mike@karemi.fsnet.co.uk
'Check for LB version
if (left$(Version$,1) <> "4") then
    notice "Sorry...";chr$(13);"You need at least version 4 of LibertyBASIC"
    end
end if
'
global minVal
global maxVal
numSliders=2 '3
dim OutputVal(numSliders) 'array to store values from slider

minVal=0     'Minimum Slider Value
maxVal=100   'Maximum Slider Value

WindowWidth = 300
WindowHeight = 250
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)

graphicbox #w.gb1,  7,  7,  15,  200
graphicbox #w.gb2,  30,  7,  15,  200
'graphicbox #w.gbn,  x,  y,  w,  h
staticText #w.st1, "", 120, 35, 130, 25
staticText #w.st2, "", 120, 70, 130, 25
'staticText #w.stn, "", x, y, w, h
nomainwin
    open "Multiple Slider Controls (LB4)" for window as #w
    #w, "trapclose [quit]"
    #w, "font technical 12"

    hgb(1)=hwnd(#w.gb1)
    hgb(2)=hwnd(#w.gb2)
'    hgb(n)=hwnd(#w.gbn)

    #w.gb1, "when leftButtonDown [leftBdown];when leftButtonUp [leftBmove];when leftButtonMove [leftBmove]"
    #w.gb2, "when leftButtonDown [leftBdown];when leftButtonUp [leftBmove];when leftButtonMove [leftBmove]"
'    #w.gb3, "when leftButtonDown [leftBdown];when leftButtonUp [leftBmove];when leftButtonMove [leftBmove]"

    #w.st1, "Slider1 Value=";minVal 'Display OutputVal
    #w.st2, "Slider2 Value=";minVal 'Display OutputVal
'    #w.stn, "Slidern Value=";minVal 'Display OutputVal

for i=1 to numSliders
varh$="#w.gb"+str$(i)
    #varh$, "down"
    #varh$, "fill yellow"
    #varh$, "size 2;color red;place 1 185 ; backcolor blue "
    #varh$, "boxfilled 13 198"
    #varh$, "flush"
next i

wait

[leftBdown] 'left mouse button down
    calldll #user32, "GetFocus", hFocus As long
    for i=1 to numSliders
        if hgb(i)=hFocus then exit for
    next i
    slider$=str$(i)
wait


[leftBmove] 'left mouse button move
    OutputVal(val(slider$))=getSliderValue(slider$)
    varh$="#w.st"+slider$ 
    #varh$, "Slider";slider$;" Value=";OutputVal(val(slider$)) 'Display OutputVal
wait

function getSliderValue(slider$)
    if (MouseY < minVal) then MouseY=minVal+1
    if (MouseY > 185) then MouseY=185
    varh$="#w.gb"+slider$
    #varh$, "segment segNo;delsegment segNo-1"
    #varh$, "fill yellow;place 1 ";MouseY;";boxfilled 13 ";MouseY+13
    #varh$, "flush"
    getSliderValue=maxVal-int((MouseY/185)*(maxVal)) 'Calculate Slider Value
end function

[quit]
    close #w
end


Home

Text To Speech - Pendl

Novice Puzzles - Terra

Slider Controls - Bradbury

Symmetrical Paint - Nally

Programming Style - Brossman

Progress Simulator - Rahman

CD Menus - Sweet

Media File Search - Sweet

Why Liberty BASIC? - Sweet

Submission Guildlines

Newsletter Help

Index